<template>
  <div v-loading="loading" element-loading-text="登录中..." element-loading-spinner="el-icon-loading" element-loading-background="rgba(0, 0, 0, 0.6)" class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
      <!-- 头像区域 -->
      <div v-if="TxStatus" class="avatar-box">
        <img src="https://img0.baidu.com/it/u=422338974,563456995&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=666" alt="">
      </div>

      <div class="title-container">

        <h3 class="title"><i class="el-icon-sunny">   美奥口腔管理系统</i></h3>
      </div>
<!--          <video-->
<!--        style="position: absolute; z-index: -1; width: 1900px"-->
<!--        class="mp41"-->
<!--        src="../assets/img/背景视频.mp4"-->
<!--        autoplay="autoplay"-->
<!--        loop="loop"-->
<!--        muted="muted"-->
<!--    ></video>-->
      <!--用户名-->
      <el-form-item prop="username">
        <span class="el-icon-user">
        </span>
        <el-input ref="username"  v-model="loginForm.username" placeholder="Username" name="username" type="text" tabindex="1" auto-complete="on" />
      </el-form-item>
      <!--密码-->
      <el-form-item prop="password">
        <span class="el-icon-unlock">
        </span>
        <el-input :key="passwordType" ref="password" v-model="loginForm.password" :type="passwordType" placeholder="Password" name="password" tabindex="2" auto-complete="on" @keyup.enter.native="login" />
        <span class="show-pwd" @click="showPwd">
        </span>
      </el-form-item>

<!--      登录按钮-->
      <p class="login_agree"><el-checkbox>我已阅读并同意<a>《用户服务协议》</a>、<a>《隐私权政策》</a></el-checkbox></p>
      <div>
        <el-button type="success" style="width:100%;margin-top:15px;" @click.native.prevent="login">登录</el-button>
      </div>

<!--      最下面的提示-->
      <div style="margin-top: 15px">
        <span style="color:#e71616;cursor:pointer;margin-left: 440px; margin-top:45px" @click="ForgetPassword">忘记密码？</span></div>

      <div class="tips">
        <span style="margin-left:120px;"><span style="color:#0c92e7;cursor:pointer;margin-left: 30px"  v-on:mouseover="changeColor">验证码登录？</span>或
          <span style="color:#10ade0;cursor:pointer; text-align:right;" @click="register">注册？</span>
          </span>
      </div>

    </el-form>
  </div>
</template>

<script>
// 引入去除空格工具
import { validUsername } from '@/utils/validate'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (value.length==0) {
        callback(new Error('用户名不能为空！'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 3) {
        callback(new Error('密码最少为3位字符！'))
      } else {
        callback()
      }
    }
    return {
      // 头像状态
      TxStatus: true,
      loginForm: {
        username: '',
        password: ''
      },
      // 登录规则
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    //忘记密码
    ForgetPassword(){
      console.log('111')
      this.$router.push({
              path: "/forget",
            });
    },
    changeColor() {
      this.style.color = 'red';
    },

    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    // 登录业务
    // handleLogin() {
    //   this.$refs.loginForm.validate((valid) => {
    //     // 如果符合验证规则
    //     if (valid) {
    //       this.loading = true
    //       setTimeout(() => {
    //         this.$store.dispatch('user/login', this.loginForm).then(() => {
    //           this.$router.push({ path: this.redirect || '/' })
    //           this.loading = false
    //         }).catch(() => {
    //           this.loading = false
    //         })
    //       }, 1500)
    //     } else {
    //       console.log('error submit!!')
    //       return false
    //     }
    //   })
    // },
    login() {
      // console.log("账号" + this.userForm.userName);
      // console.log("密码" + this.userForm.passWord);
      if (this.loginForm.username !== "" || this.loginForm.password !== "") {
        //axios登录请求
        this.$axios.post(this.$settings.BASE_URL + 'user/login/login/', {
          username: this.loginForm.username,
          password: this.loginForm.password,
        }).then((Response) => {
          let type = ''
          let user = Response.data.user;
          console.log(Response.data)
          console.log('我在这哦')
          if (Response.data.code === 100) {
            this.$setSessionStorage("user", user);
            this.$setSessionStorage("token", Response.data.token);
            this.$setSessionStorage("icon", Response.data.user.icon);
            this.$setSessionStorage("gender", Response.data.gender);
            this.$setSessionStorage("mobile", Response.data.mobile);
            this.$setSessionStorage("user_type", Response.data.user_type);
            this.$setSessionStorage("user_id", Response.data.user_id);
            localStorage.setItem("authorization", Response.data.token);
            this.$router.push({
              path: "/index",
            });
            type = 'success'
          } else {
            type = 'warning'
          }
          this.$message({
            message: Response.data.msg,
            type: type,
            duration: 1000
          })
        })
            .catch((error) => {
              console.error(error);
            });
      } else {
        this.$message({
          message: '账号或密码不能为空',
          type: 'warning',
          duration: 1000
        })
      }
      console.log("账号" + this.loginForm.username);
      console.log("密码" + this.loginForm.password);
      if (this.loginForm.username == "") {
        alert("账号不能为空");
      } else if (this.loginForm.password == "") {
        alert("密码不能为空");
      }
    },
    // 注册业务
    register() {
      console.log('123')
      this.$router.push({ name: 'register' })
    }
  }
}
</script>

<style lang="scss">
body{
	margin:0;
	padding:0;
	border:0
}

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;
@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
display: flex;
  justify-content: center;
  align-items: stretch;
  background-image: url('../assets/img/111.png');
  background-size: 100% 100%;
  height: 100%;
  width: 100%;
  position: fixed;

   // 头像
  .avatar-box {
    margin: 0 auto;
    width: 120px;
    height: 120px;
    border-radius: 50%;
    border: 1px solid #40ff59;
    box-shadow: 0 0 10px #40ff70;
    position: relative;
    bottom: 20px;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
    }
  }

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;

  }

  .tips {
    font-size: 18px;

    color: #000;
    margin-top: 40px;
  }

  //.svg-container {
  //  padding: 6px 5px 6px 15px;
  //  color: $dark_gray;
  //  vertical-align: middle;
  //  width: 30px;
  //  display: inline-block;
  //}

  .title-container {
    position: relative;

    .title {
      font-size: 30px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: 500;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
.el-button--success,.el-button--success:focus,.el-button--success.is-active, .el-button--success:active{background: #23C6C8;}
.el-button--warning,.el-button--warning:focus,.el-button--warning.is-active, .el-button--warning:active{background: #FCB065;}
.el-button--success:hover {
    background: #8cd8da;
    border-color: #8cd8da;
    color: #FFF;
}
.el-button--warning:hover {
    background: #f5d7b9;
    border-color: #f5d7b9;
    color: #FFF;
}

</style>